package cn.vconinfo.base.oss;

import java.util.HashMap;

public class ContentTypeUtil {

    private static volatile ContentTypeUtil singleton = null;

    private final String[][] MIME_StrTable = new String[][] { { ".3gp", "video/3gpp" }, { ".asf", "video/x-ms-asf" }, { ".avi", "video/x-msvideo" }, { ".m4u", "video/vnd.mpegurl" }, { ".m4v", "video/x-m4v" }, { ".mov", "video/quicktime" }, { ".mp4", "video/mp4" }, { ".mpg4", "video/mp4" }, { ".mpe", "video/x-mpeg" }, { "", "video/mpg" }, { ".mpeg", "video/mpg" }, { ".mpg", "video/mpg" }, { ".m3u", "audio/x-mpegurl" }, { "", "audio/mp4a-latm" }, { ".m4a", "audio/mp4a-latm" },
            { ".m4b", "audio/mp4a-latm" }, { ".m4p", "audio/mp4a-latm" }, { ".mp2", "x-mpeg" }, { ".mp3", "audio/x-mpeg" }, { ".mpga", "audio/mpeg" }, { ".ogg", "audio/ogg" }, { ".rmvb", "audio/x-pn-realaudio" }, { ".wav", "audio/x-wav" }, { ".wma", "audio/x-ms-wma" }, { ".wmv", "audio/x-ms-wmv" }, { "", "text/plain" }, { ".c", "text/plain" }, { ".java", "text/plain" }, { ".conf", "text/plain" }, { ".cpp", "text/plain" }, { ".h", "text/plain" }, { ".prop", "text/plain" },
            { ".rc", "text/plain" }, { ".sh", "text/plain" }, { ".log", "text/plain" }, { ".txt", "text/plain" }, { ".xml", "text/plain" }, { ".html", "text/html" }, { ".htm", "text/html" }, { ".css", "text/css" }, { ".jpg", "image/jpeg" }, { ".jpeg", "image/jpeg" }, { ".bmp", "image/bmp" }, { ".gif", "image/gif" }, { ".png", "image/png" }, { ".ico", "image/x-icon" }, { "", "application/octet-stream" }, { ".bin", "application/octet-stream" }, { ".class", "application/octet-stream" },
            { ".exe", "application/octet-stream" }, { "class", "application/octet-stream" }, { ".apk", "application/vnd.android.package-archive" }, { ".doc", "application/msword" }, { ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }, { ".xls", "application/vnd.ms-excel" }, { ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, { ".gtar", "application/x-gtar" }, { ".gz", "application/x-gzip" }, { ".jar", "application/java-archive" },
            { ".js", "application/x-javascript" }, { ".mpc", "application/vnd.mpohun.certificate" }, { ".msg", "application/vnd.ms-outlook" }, { ".pdf", "application/pdf" }, { "", "application/vnd.ms-powerpoint" }, { ".pps", "application/vnd.ms-powerpoint" }, { ".ppt", "application/vnd.ms-powerpoint" }, { ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" }, { ".rtf", "application/rtf" }, { ".tar", "application/x-tar" }, { ".tgz", "application/x-compressed" },
            { ".wps", "application/vnd.ms-works" }, { ".z", "application/x-compress" }, { ".zip", "application/x-zip-compressed" } };

    static HashMap<String, String> mimeMapKeyIsContentType = null;

    static HashMap<String, String> mimeMapKeyIsExpands = null;

    private ContentTypeUtil() {
    }

    public static ContentTypeUtil getSingleton() {
        if (singleton == null) {
            synchronized (ContentTypeUtil.class) {
                if (singleton == null) {
                    singleton = new ContentTypeUtil();
                }
            }
        }

        return singleton;
    }

    private HashMap<String, String> CreateMIMEMapKeyIsContentType() {
        HashMap<String, String> mimeHashMap = new HashMap<>();

        for (int i = 0; i < this.MIME_StrTable.length; ++i) {
            if (this.MIME_StrTable[i][1].length() > 0 && !mimeHashMap.containsKey(this.MIME_StrTable[i][1])) {
                mimeHashMap.put(this.MIME_StrTable[i][1], this.MIME_StrTable[i][0]);
            }
        }

        return mimeHashMap;
    }

    private HashMap<String, String> CreateMIMEMapKeyIsExpands() {
        HashMap<String, String> mimeHashMap = new HashMap<>();

        for (int i = 0; i < this.MIME_StrTable.length; ++i) {
            if (this.MIME_StrTable[i][0].length() > 0 && !mimeHashMap.containsKey(this.MIME_StrTable[i][0])) {
                mimeHashMap.put(this.MIME_StrTable[i][0], this.MIME_StrTable[i][1]);
            }
        }

        return mimeHashMap;
    }

    private HashMap<String, String> getMIMEMapKeyIsContentType() {
        if (mimeMapKeyIsContentType == null) {
            mimeMapKeyIsContentType = this.CreateMIMEMapKeyIsContentType();
        }

        return mimeMapKeyIsContentType;
    }

    private HashMap<String, String> getMIMEMapKeyIsExpands() {
        if (mimeMapKeyIsExpands == null) {
            mimeMapKeyIsExpands = this.CreateMIMEMapKeyIsExpands();
        }

        return mimeMapKeyIsExpands;
    }

    public String getExtensionByCutStr(String fileName) {
        int lastIndexOfDot = fileName.lastIndexOf(".");
        if (lastIndexOfDot < 0) {
            return "";
        } else {
            String extension = fileName.substring(lastIndexOfDot + 1);
            return extension;
        }
    }

    public String getContentTypeByExpansion(String expansionName) {
        String expansion = expansionName;
        if (!expansionName.startsWith(".")) {
            expansion = "." + expansionName;
        }

        HashMap<String, String> expandMap = this.getMIMEMapKeyIsExpands();
        String contentType = (String) expandMap.get(expansion);
        return contentType == null ? "" : contentType;
    }

    public String getExpandByContentType(String contentType) {
        HashMap<String, String> expandMap = this.getMIMEMapKeyIsContentType();
        String expands = (String) expandMap.get(contentType);
        return expands == null ? "" : expands;
    }

    public static void main(String[] args) {
        System.out.println(getSingleton().getContentTypeByExpansion(".gif"));
        System.out.println(getSingleton().getContentTypeByExpansion("png"));
        System.out.println(getSingleton().getExpandByContentType("image/png"));
        String extName = getSingleton().getExtensionByCutStr("a.pdf");
        System.out.println(getSingleton().getContentTypeByExpansion(extName));
        System.out.println(getSingleton().getContentTypeByExpansion("psd"));
    }

}
